package vn.lumi.smarthome.network;

import java.util.Arrays;

import vn.lumi.smarthome.R;
import vn.lumi.smarthome.io.database.TBL_Device;
import vn.lumi.smarthome.io.database.TBL_Scene;
import vn.lumi.smarthome.io.model.Scene;

import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Parcel;
import android.util.Log;

public class TurnOnScene extends AppMsg {

	// constants
	private static final int LENGTH = 5;

	// attributes
	private int Scene_id = 0;

	public TurnOnScene(int Msg_id, int uid, int sid, int scene_id) {
		super(NetMessage.CMD_APP_ACTIVATE_SCENE, Msg_id, LENGTH, uid, sid);
		// TODO Auto-generated constructor stub
		Scene_id = scene_id;
	}

	@Override
	public byte[] generateByteArray() {
		// TODO Auto-generated method stub
		byte[] result = new byte[LENGTH];
		result[0] = (byte) this.Cmd_id;
		result[1] = (byte) this.Msg_id;
		result[2] = (byte) this.user_id;
		result[3] = (byte) this.session_id;
		result[4] = (byte) this.Scene_id;
		Log.e("ban tin", "TurnOnScene: " + Arrays.toString(result));
		return result;
	}

	@Override
	public int describeContents() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public void writeToParcel(Parcel dest, int flags) {
		// TODO Auto-generated method stub
		dest.writeInt(this.Cmd_id);
		dest.writeInt(this.length);
		dest.writeInt(this.Msg_id);
		dest.writeInt(this.session_id);
		dest.writeInt(this.user_id);
		dest.writeInt(this.Scene_id);
	}

	@Override
	public void doIfSendSuccessful(Context context) {
		// TODO Auto-generated method stub
		// find room of scene
		int roomID = 0;
		Uri uri = TBL_Scene.CONTENT_URI_SCENE;
		String[] projection = new String[] { TBL_Scene.ROOM_ID };
		// Hieu sua
		// String selection=TBL_Device._ID+"=?";
		String selection = TBL_Scene._ID + "=?";
		// End chinh sua
		String[] selectionArgs = new String[] { this.Scene_id + "" };
		Cursor cursor = context.getContentResolver().query(uri, projection,
				selection, selectionArgs, null);
		if (cursor != null) {
			if (cursor.moveToFirst())
				roomID = cursor
						.getInt(cursor.getColumnIndex(TBL_Scene.ROOM_ID));
		} else
			return;

		Log.d("ActivateScene", "room_id=" + roomID);

		// find all scenes of this room
		projection = new String[] { TBL_Scene._ID, TBL_Scene.STATE };
		selection = TBL_Scene.ROOM_ID + "=?";
		selectionArgs = new String[] { roomID + "" };
		cursor = context.getContentResolver().query(uri, projection, selection,
				selectionArgs, null);
		if (cursor == null)
			return;
		cursor.moveToFirst();
		while (!cursor.isAfterLast()) {
			int sceneID = cursor.getInt(cursor.getColumnIndex(TBL_Scene._ID));
			int sceneState = cursor.getInt(cursor
					.getColumnIndex(TBL_Scene.STATE));
			if (sceneID != this.Scene_id && sceneState == Scene.SCENE_STATE_ON) {
				// set this scene to off
				ContentValues values = new ContentValues();
				values.put(TBL_Scene.STATE, Scene.SCENE_STATE_OFF);
				selection = TBL_Scene._ID + "=?";
				selectionArgs = new String[] { sceneID + "" };
				context.getContentResolver().update(uri, values, selection,
						selectionArgs);
				Log.d("ActivateScene", "turn off sceneID=" + sceneID);
			} else if (sceneID == this.Scene_id) {
				// set this scene to on
				ContentValues values = new ContentValues();
				values.put(TBL_Scene.STATE, Scene.SCENE_STATE_ON);
				selection = TBL_Scene._ID + "=?";
				selectionArgs = new String[] { sceneID + "" };
				context.getContentResolver().update(uri, values, selection,
						selectionArgs);
				Log.d("ActivateScene", "turn on sceneID=" + sceneID);

			}
			cursor.moveToNext();
		}

		// Hieu them vao
		// inform UI
		Intent intent = new Intent();
		intent.setAction(context
				.getString(R.string.txt_broadcast_turnOn_scene_successful));
		context.sendBroadcast(intent);
	}

	@Override
	public void doIfSendFail(Context context) {
		// TODO Auto-generated method stub
		// turn off this scene
		Uri uri = TBL_Scene.CONTENT_URI_SCENE;
		ContentValues values = new ContentValues();
		values.put(TBL_Scene.STATE, Scene.SCENE_STATE_OFF);
		String selection = TBL_Scene._ID + "=?";
		String[] selectionArgs = new String[] { this.Scene_id + "" };
		context.getContentResolver().update(uri, values, selection,
				selectionArgs);
		// Hieu them vao
		// inform UI
		Intent intent = new Intent();
		intent.setAction(context
				.getString(R.string.txt_broadcast_turnOn_scene_failed));
		context.sendBroadcast(intent);
	}

	@Override
	public void readFromParcel(Parcel in) {
		// TODO Auto-generated method stub
		Cmd_id = in.readInt();
		length = in.readInt();
		Msg_id = in.readInt();
		session_id = in.readInt();
		user_id = in.readInt();
		Scene_id = in.readInt();
	}

	@Override
	public boolean isCorrectReply(byte[] input, int length) {
		// TODO Auto-generated method stub
		boolean result = false;
		HC_ACK ack = new HC_ACK();
		if (ack.isCorrectFormat(input, length)) {
			ack.parseMessage(input, length);
			if (ack.getMsg_id() == this.Msg_id)
				result = true;
		}
		return result;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see vn.lumi.smarthome.network.AppMsg#isCorrectBusy(byte[], int)
	 */
	@Override
	public boolean isCorrectBusy(byte[] input, int length) {
		// TODO Auto-generated method stub
		boolean result = false;
		HC_ACK_BUSY ack_busy = new HC_ACK_BUSY();
		if (ack_busy.isCorrectFormatBusy(input, length)) {
			ack_busy.parseMessage(input, length);
			if (ack_busy.getMsg_id() == this.Msg_id)
				result = true;
		}
		return result;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see vn.lumi.smarthome.network.AppMsg#doIfHcBusying()
	 */
	@Override
	public void doIfHcBusying() {
		// TODO Auto-generated method stub

	}
}
